from app import db
from datetime import datetime

class RegulationService(db.Model):
    """调节服务数据模型"""
    __tablename__ = 'regulation_services'
    
    id = db.Column(db.Integer, primary_key=True)
    year = db.Column(db.Integer, nullable=False, comment='核算年份')
    gepCity = db.Column(db.String(50), nullable=False, comment='市级地区名称')
    gepDistrict = db.Column(db.String(50), nullable=False, comment='区级地区名称')
    gepTown = db.Column(db.String(50), nullable=False, comment='镇级地区名称')
    outWaterSourceConservation = db.Column(db.Float, comment='水源涵养价值（万元）')
    outSoilConservation = db.Column(db.Float, comment='土壤保持价值（万元）')
    outNegativeOxygenIons = db.Column(db.Float, comment='负氧离子价值（万元）')
    outAirPurification = db.Column(db.Float, comment='空气净化价值（万元）')
    outWaterPurification = db.Column(db.Float, comment='水质净化价值（万元）')
    outCarbonSequestration = db.Column(db.Float, comment='固定二氧化碳价值（万元）')
    outOxygenRelease = db.Column(db.Float, comment='释氧价值价值（万元）')
    outClimateRegulation = db.Column(db.Float, comment='气候调节价值（万元）')
    outCalcFloodRegulation = db.Column(db.Float, comment='洪水调蓄价值（万元）')
    outCalcBiodiversityProtection = db.Column(db.Float, comment='生物多样性保护价值（万元）')
    outCalcNoiseReduced = db.Column(db.Float, comment='噪声消减价值（万元）')
    creator = db.Column(db.String(50), comment='数据创建者用户名')
    updater = db.Column(db.String(50), comment='数据更新者用户名')
    created_at = db.Column(db.DateTime, default=datetime.utcnow, comment='数据创建时间')
    updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, comment='数据最后更新时间')
    
    def __repr__(self):
        return f'<RegulationService {self.year}-{self.gepCity}-{self.gepDistrict}-{self.gepTown}>'
    
    def to_dict(self):
        return {
            'id': self.id,
            'year': self.year,
            'gepCity': self.gepCity,
            'gepDistrict': self.gepDistrict,
            'gepTown': self.gepTown,
            'outWaterSourceConservation': self.outWaterSourceConservation,
            'outSoilConservation': self.outSoilConservation,
            'outNegativeOxygenIons': self.outNegativeOxygenIons,
            'outAirPurification': self.outAirPurification,
            'outWaterPurification': self.outWaterPurification,
            'outCarbonSequestration': self.outCarbonSequestration,
            'outOxygenRelease': self.outOxygenRelease,
            'outClimateRegulation': self.outClimateRegulation,
            'outCalcFloodRegulation': self.outCalcFloodRegulation,
            'outCalcBiodiversityProtection': self.outCalcBiodiversityProtection,
            'outCalcNoiseReduced': self.outCalcNoiseReduced,
            'creator': self.creator,
            'updater': self.updater,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None
        }